package com.bianmaba.oauth2.clientcredentials;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.client.token.AccessTokenRequest;
import org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider;
import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider;
import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Arrays;
import java.util.HashMap;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {PasswordAuthServerApplication.class})
public class OAuth2RestTemplateTester {

    public OAuth2ProtectedResourceDetails resourceOwnerPasswordResourceDetails() {

        ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
        resource.setAccessTokenUri("http://localhost:8080/oauth/token");
        resource.setClientId("client_1");
        resource.setClientSecret("123456");
        resource.setGrantType("Password");
        resource.setScope(Arrays.asList("select"));

        resource.setUsername("user_1");
        resource.setPassword("123456");
        return resource;
    }

    @Test
    public void getProductOK() throws Exception {


        ResourceOwnerPasswordAccessTokenProvider provider = new ResourceOwnerPasswordAccessTokenProvider();
        OAuth2AccessToken accessToken = provider.obtainAccessToken(resourceOwnerPasswordResourceDetails(), new DefaultAccessTokenRequest());

        OAuth2RestTemplate template = new OAuth2RestTemplate(resourceOwnerPasswordResourceDetails(), new DefaultOAuth2ClientContext(accessToken));
        OAuth2AccessToken qq = template.getAccessToken();
        HashMap xxx = template.getForObject("http://localhost:8080/product/1", HashMap.class);
        Assert.assertEquals(xxx.get("id"), "1");
    }

    @Test
    public void getOrderOK1() throws Exception {
        AccessTokenRequest accessTokenRequest = new DefaultAccessTokenRequest();
        ResourceOwnerPasswordAccessTokenProvider provider = new ResourceOwnerPasswordAccessTokenProvider();

        OAuth2RestTemplate template = new OAuth2RestTemplate(resourceOwnerPasswordResourceDetails(), new DefaultOAuth2ClientContext(accessTokenRequest));
        template.setAccessTokenProvider(provider);

        HashMap xxx = template.getForObject("http://localhost:8080/order/1", HashMap.class);
        Assert.assertEquals(xxx.get("id"), "1");
    }

    @Test
    public void getOrderOK2() throws Exception {

        ResourceOwnerPasswordAccessTokenProvider provider = new ResourceOwnerPasswordAccessTokenProvider();
        OAuth2AccessToken accessToken = provider.obtainAccessToken(resourceOwnerPasswordResourceDetails(), new DefaultAccessTokenRequest());

        OAuth2RestTemplate template = new OAuth2RestTemplate(resourceOwnerPasswordResourceDetails(), new DefaultOAuth2ClientContext(accessToken));
        template.setAccessTokenProvider(provider);

        HashMap xxx = template.getForObject("http://localhost:8080/order/1", HashMap.class);
        Assert.assertEquals(xxx.get("id"), "1");
    }
}
